package com.frank.demo.mq.kafka;


import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.rocketmq.client.exception.MQClientException;

import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

//生产者
public class Producer_client {

    public static void main(String[] args) throws MQClientException, InterruptedException {
        Properties properties = new Properties();
        properties.put("bootstrap.servers", "127.0.0.1:9092");
        properties.put("acks", "all");
        properties.put("retries", 0);
        properties.put("batch.size", 16384);
        properties.put("linger.ms", 1);
        properties.put("buffer.memory", 33554432);
        properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        Producer<String, String> producer =   new KafkaProducer<String, String>(properties);

        CountDownLatch latch = new CountDownLatch(1);
        Runnable runner1 = new Runnable() {
            public void run() {
                for (int i = 0; ; i++) {
                    String msg = "Message " + i;
                    producer.send(new ProducerRecord<String, String>("test01", msg));
                    System.out.println("Sent:" + msg);
                    try {
                        Thread.sleep(30);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
//                latch.countDown();
            }
        };

        Executor executor = Executors.newFixedThreadPool(2);
        executor.execute(runner1);
        latch.await();
        producer.close();
    }
}
